﻿using Administratie_lib;
using DataAccess;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SedatGroep6
{
    public partial class Assemblage : Form
    {
        private Database db;
        private Bestelling bestelling;
        public Assemblage()
        {
            InitializeComponent();
            db = new Database();
            bestelling = new Bestelling();
        }

        private void Assemblage_Load(object sender, EventArgs e)
        {
            UpdateOrders();
        }

        private void lbOrders_SelectedIndexChanged(object sender, EventArgs e)
        {
            lbProducten.Items.Clear();
            if (lbOrders.SelectedIndex != -1)
            {
                DataTable producten = bestelling.Zoek(GetOrderID());
                foreach (DataRow productdetails in producten.Rows)
                {
                    lbProducten.Items.Add(productdetails["Id"] + " - "+productdetails["Naam"]+" - "+productdetails["Voorraad"]+"x");
                }
            }
            gbUpdateStatus.Visible = true;
        }

        private void btUpdate_Click(object sender, EventArgs e)
        {
            string update = "";
            if (rbBehandeling.Checked)
                update = "In behandeling";
            else if (rbWachten.Checked)
                update = "Wacht op voorraad";
            else if (rbLeverancier.Checked)
                update = "Aan leverancier aangeboden";
            else if (rbPakketdienst.Checked)
                update = "Aan pakketdienst aangeboden";

            Assemblage_lib.I_Assemblage assemblage = new Assemblage_lib.Assemblage();
            assemblage.Assembleer(update, GetOrderID());

            UpdateOrders();
        }

        private int GetOrderID()
        {
            string orderID = lbOrders.GetItemText(lbOrders.SelectedItem);
            string[] orderIDSplit = orderID.Split(' ');
            return Convert.ToInt32(orderIDSplit[1]);
        }

        private void UpdateOrders()
        {
            gbUpdateStatus.Visible = false;
            lbProducten.Items.Clear();
            lbOrders.Items.Clear();
            DataTable resultOrder = bestelling.Zoek();

            foreach (DataRow row in resultOrder.Rows)
            {
                double totalPrice = 0;
                DataTable prices = bestelling.Zoek(Convert.ToInt32(row["Id"]));
                foreach (DataRow priceRows in prices.Rows)
                {
                    totalPrice += (Convert.ToDouble(priceRows["aantal"]) * Convert.ToInt32(priceRows["prijs"]));
                }
                lbOrders.Items.Add("Order " + row["Id"].ToString() + " - €" + totalPrice);
            }
        }

        private void btRefresh_Click(object sender, EventArgs e)
        {
            UpdateOrders();
        }
    }
}
